const Response = require('node-spider').Response
const Request = require('node-spider').Request;
let Spiders = require('node-spider').Spider;
var count = 0;
module.exports.Spider = class extends Spiders{
    constructor(){
        super();
        this.urls=['http://zuidazy.net/'];
        this.count=0;
    }
    //yield关键字返回数据或者新的请求
    *parse(response){
        console.log("当前this"+this);
        let $ = response.selector;
        //
        var request = undefined;
        $('.pages a').each((index,item)=>{
            if($(item).text().trim()=='下一页'){
                console.log('下一页地址:'+response.url);
                request = new Request({url:response.url});
                request.joinUrl($(item).prop('href'));
            }
        });

        if (request!=undefined){
            console.log("准备下一页"+(count++));
            yield request;
        }


        let res = $('.xing_vb li');
        res.children().last().remove();
        for (let i=0;i<res.length-2;i++){
            let sliceUrl = res.find("a").eq(i).prop("href");
            yield new Request({url:response.url,callback:this.parse_content}).joinUrl(sliceUrl);

        }

    }
    //解析内容
    *parse_content(response=new Response()){

        let $ = response.selector;
        let summary = $('.vodinfobox').find('li');
        yield summary.eq(0).text()+summary.eq(2).text()+summary.eq(3).text();
    }
}